package com.wsj.customtag;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wsj.web.constant.map.MapHomeSiteContent;

public class HostForwardTag extends SimpleTagSupport
{
	
	//网购通主页
	private String URLForHost;
	
	//网购通二级域名跳转页面.
	private String URLForCustomer="/dealer_index.do";
	
	//招商通二级页面.
	private String URLForZST="/zst_index.do";
	
	//总站主页
	private String URLForHomeSiteIndex="/index_html.do";
	
	//分站首页
	private String URLForHomeSiteAreaIndex="/homeSiteAreaNewsIndex.do";
	
	Log logger =LogFactory.getLog(this.getClass());

	@Override
	public void doTag() throws JspException, IOException
	{
		
		PageContext pageContext = (PageContext)this.getJspContext();
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        HttpServletResponse response =(HttpServletResponse)pageContext.getResponse();
        String servername=request.getServerName();
        logger.info("------>>>>>>>>> servername:" + String.valueOf(servername));
        String patternStr = "wsj.com";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher =null;
        if(servername!=null && !servername.equals(""))
        {
        	matcher = pattern.matcher(servername);
        }
        
		if(matcher!=null && matcher.find())
		{
			String scd = servername.substring(0,matcher.start());
			try{
				logger.info("\n----->>  qiyejibiaoId:"+scd);   
				
				//总站检测
				boolean areaIndexOk=this.checkForwardForMainPage(request,response,scd);
                //总站栏目总站检测
				if(areaIndexOk && (scd!=null  && MapHomeSiteContent.getColumnMap().containsKey(scd)))
				{
					areaIndexOk=this.checkMapHomeSiteContent(request,response,scd);
				}
				
				//不存在 "."
				if(scd!=null&&!scd.equals("")&&scd.indexOf(".")==-1){
					this.forword(request, response, URLForHomeSiteIndex);

				}	
			
			}catch(Exception e)
			{
				logger.error("\n\n跳转异常:"+e.toString());
				throw new SkipPageException();
			}
			
		}
		//会员域名.
		else
		{
			try{
				RequestDispatcher dispatcher = request.getRequestDispatcher(URLForHost);
				dispatcher.forward(request, response);
			}catch(Exception e){
				logger.error("\n\n跳转异常:"+e.toString());
			   throw new SkipPageException();
			}
			
		}

	}
	
	
	
	
	private void forword(HttpServletRequest request,HttpServletResponse response,String todo)
	{
		try {
			RequestDispatcher  rs  =request.getSession().getServletContext().getRequestDispatcher(todo);
			rs.forward(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/****
	 * 主站栏目的跳转 ,返回true 表示符合跳转分站的条件
	 * @param scd
	 * @return
	 */
	private boolean checkMapHomeSiteContent(HttpServletRequest request,HttpServletResponse response,String scd) 
	{
		boolean isok=true;
		try {
			Map<String, String> columnMap=MapHomeSiteContent.getColumnMap();
			if(scd==null||scd.equals(""))
			{
				isok =false;
				//response.sendRedirect(URLForHomeSiteIndex); 
				this.forword(request, response, URLForHomeSiteIndex);
				
			}
			//资讯中心
			else if(scd!=null && "news.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("news.")); 
				this.forword(request, response, columnMap.get("news."));
			}
			//招商中心
			else if(scd!=null && "zs.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("zs.")); 
				this.forword(request, response, columnMap.get("zs."));
			}
			//品牌广场
			else if(scd!=null && "brand.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("brand.")); 
				this.forword(request, response, columnMap.get("brand."));
			}
			//产品信息
			else if(scd!=null && "product.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("product.")); 
				this.forword(request, response, columnMap.get("product."));
			}
			//商机信息
			else if(scd!=null && "trade.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("trade.")); 
				this.forword(request, response, columnMap.get("trade."));
			}
			//经销商
			else if(scd!=null && "dealer.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("dealer.")); 
				this.forword(request, response, columnMap.get("dealer."));
			}
			//主题杂志
			else if(scd!=null && "zine.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("zine.")); 
				this.forword(request, response, columnMap.get("zine."));
			}
			//专题直播
			else if(scd!=null && "fea.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("fea.")); 
				this.forword(request, response, columnMap.get("fea."));
			}
			//购车商城
			else if(scd!=null && "mall.".equals(scd))
			{
				isok=false;
//				response.sendRedirect(columnMap.get("mall.")); 
				this.forword(request, response, columnMap.get("mall."));
			}
			//行业论坛
			else if(scd!=null && "bbs.".equals(scd))
			{
				isok=false;
				response.sendRedirect(columnMap.get("bbs.")); 
//				this.forword(request, response, columnMap.get("bbs."));
			}

			
		} catch (Exception e) {
			
			isok=false;
			e.printStackTrace();
		}
		
		return isok;
		
	}
	/****
	 * 主站的跳转 ,返回true 表示符合跳转分站的条件
	 * @param scd
	 * @return
	 */
	private boolean checkForwardForMainPage(HttpServletRequest request,HttpServletResponse response,String scd) 
	{
		boolean isok=true;
		try {
			Map<String, String> columnMap=MapHomeSiteContent.getColumnMap();
			if(scd==null||scd.equals(""))
			{
				isok =false;
				response.sendRedirect(URLForHomeSiteIndex); 
				
			}
			//网购通首页
			else if(scd!=null && "sale.".equals(scd))
			{
				isok=false;
				response.sendRedirect(URLForHost); 
			}
			// 网购通首页
			else if(scd!=null && "mall.".equals(scd))
			{
				isok=false;
				this.forword(request, response, columnMap.get("mall."));
				//response.sendRedirect(URLForHost);
			}
			//主站首页
			else if(scd!=null  && scd.equals("www."))
			{
				isok=false;
				//response.sendRedirect(URLForHomeSiteIndex);
				this.forword(request, response, URLForHomeSiteIndex);
			}
			
		} catch (IOException e) {
			
			isok=false;
			e.printStackTrace();
		}
		
		return isok;
		
	}



	
	public String getURLForCustomer() {
		return URLForCustomer;
	}

	public void setURLForCustomer(String forCustomer) {
		URLForCustomer = forCustomer;
	}

	public String getURLForHost() {
		return URLForHost;
	}

	public void setURLForHost(String forHost) {
		URLForHost = forHost;
	}



	public String getURLForZST() {
		return URLForZST;
	}



	public void setURLForZST(String forZST) {
		URLForZST = forZST;
	}



	public String getURLForHomeSiteIndex() {
		return URLForHomeSiteIndex;
	}



	public void setURLForHomeSiteIndex(String forHomeSiteIndex) {
		URLForHomeSiteIndex = forHomeSiteIndex;
	}



	public String getURLForHomeSiteAreaIndex() {
		return URLForHomeSiteAreaIndex;
	}



	public void setURLForHomeSiteAreaIndex(String forHomeSiteAreaIndex) {
		URLForHomeSiteAreaIndex = forHomeSiteAreaIndex;
	}

	
	
	

}
